package com.titleevaluation.controller;

import com.titleevaluation.entity.WorkExperience;
import com.titleevaluation.service.WorkExperienceService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/applications/{applicationId}/work-experience")
public class WorkExperienceController {

    private final WorkExperienceService workExperienceService;

    public WorkExperienceController(WorkExperienceService workExperienceService) {
        this.workExperienceService = workExperienceService;
    }

    @PostMapping
    public ResponseEntity<WorkExperience> createWorkExperience(@PathVariable Long applicationId, @RequestBody WorkExperience workExperience) {
        WorkExperience createdWorkExperience = workExperienceService.createWorkExperience(applicationId, workExperience);
        return ResponseEntity.ok(createdWorkExperience);
    }

    @GetMapping
    public ResponseEntity<List<WorkExperience>> getWorkExperienceByApplicationId(@PathVariable Long applicationId) {
        List<WorkExperience> workExperience = workExperienceService.getWorkExperienceByApplicationId(applicationId);
        return ResponseEntity.ok(workExperience);
    }

    @GetMapping("/{id}")
    public ResponseEntity<WorkExperience> getWorkExperienceById(@PathVariable Long id) {
        return workExperienceService.getWorkExperienceById(id)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    @PutMapping("/{id}")
    public ResponseEntity<WorkExperience> updateWorkExperience(@PathVariable Long id, @RequestBody WorkExperience workExperience) {
        WorkExperience updatedWorkExperience = workExperienceService.updateWorkExperience(id, workExperience);
        return ResponseEntity.ok(updatedWorkExperience);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteWorkExperience(@PathVariable Long id) {
        workExperienceService.deleteWorkExperience(id);
        return ResponseEntity.noContent().build();
    }
}

